<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>权限管理流程</title>
        <jsp:include page="/views/include.jsp"></jsp:include>
        <script type="text/javascript">
        	var contextPath = '<%=request.getContextPath()%>';
        	$.parser.onComplete = function(){
            	$('body').css('visibility','visible');
            	setTimeout(function(){
	            	$('#loading-mask').remove();
            	},50);
        	};
        	$(function(){
            	$(window).resize(function(){
                	$('#mainlayout').layout('resize');
            	});
            	$('#t-flow').datagrid({onDblClickRow:function dblClickRow(rowIndex,rowData){
    				$('#myform').formid('loadit',rowData);
    				$.each($('#myform input'),function(i){
    					$(this).attr("readonly","true");
    				});
    				$("#dlg-buttons a::first-child").hide();
    				$('#dlg').dialog('setTitle','查看流程信息').dialog('open');
    			}});
        	});
        </script>
	<style type="text/css">
		.e-input{
			width:198px;
			border:1px solid #A4BED4;
			height:18px;
			line-height:18px;
		}
	</style>
	<script type="text/javascript">
	//----------------------------------------------用来格式化显示状态，是否打开和是否叶子
	var remind;
	
	$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=remind'/>", function(json){
		remind=json;
	});
	
	function remindFormatter(value){
		for(var i=0; i<remind.length; i++){
			if (remind[i].value == value) return remind[i].name;
		}
		return value;
	}
	
	//---------------------------------------------------------------------
		
		var actionUrl;
		function newItem(){
			$('#dlg').dialog('setTitle', '填写流程资料').dialog('open');
			$('#myform').form('clear');
			$.each($('#myform input'),function(i){
				$(this).removeAttr("readonly");
			});
			$("#dlg-buttons a::first-child").show();
			actionUrl = '<c:url value="/permissions/saveFlowDefine.tg"/>';
			var stepTable = document.getElementById('stepTable');
			
			while(stepTable.hasChildNodes()){
				stepTable.removeChild(stepTable.lastChild);
			}
			var newTr0 = stepTable.insertRow(stepTable.rows.length);

			newTr0.id='rowbase_'+stepTable.rows.length;									
			newTr0.insertCell(0).innerHTML = "选择";
			newTr0.insertCell(1).innerHTML = "节点名称";
			newTr0.insertCell(2).innerHTML = "是否会签";
			newTr0.insertCell(3).innerHTML = "审批人列表";
			//部门树折叠
			 $('#dealUsers888').combotree('tree').tree("collapseAll");
		}
		function editItem(){
			var t = $('#t-flow');
			var row = t.datagrid('getSelected');
			if (row){
				$('#flowId').val(row.id);
				
				$('#myform').form('clear');
				actionUrl = '<c:url value="/permissions/findFlowDefine.tg"/>';
				$('#flowform').formid('loadit',row);
				$('#flowform').form('submit', {
					url:actionUrl,
					onSubmit: function(){
						return true;
					},
					success: function(data){						
						data=eval('('+data+')');
						var tempObj = data.rows[0];
						var cnt = data.taskCnt;
						if(cnt>0){
							alert('警告,有'+cnt+'个任务在使用该流程，修改可能会影响正在进行的任务!');
						}
						var arrayCompanyId = tempObj.companyId.split(",");
						$('#companyId').combotree('setValues',arrayCompanyId);
						$('#companyId').combotree('setText',tempObj.companyName);
						//加载节点
						var nodesObj = data.details;						
						var stepTable = document.getElementById('stepTable');
						
						while(stepTable.hasChildNodes()){
							stepTable.removeChild(stepTable.lastChild);
						}
						
						var newTr0 = stepTable.insertRow(stepTable.rows.length);

						newTr0.id='rowbase_'+stepTable.rows.length;									
						newTr0.insertCell(0).innerHTML = "选择";
						newTr0.insertCell(1).innerHTML = "节点名称";
						newTr0.insertCell(2).innerHTML = "是否会签";
						newTr0.insertCell(3).innerHTML = "审批人列表";
						
						for(j=0;j<nodesObj.length;j++)
						{
							var newTr = stepTable.insertRow(stepTable.rows.length);

							newTr.id='row_'+stepTable.rows.length;
							var newTd0 = newTr.insertCell(0);						
							newTd0.innerHTML = "<input type=\"hidden\" id='id"+stepTable.rows.length+"' name=\"details["+stepTable.rows.length+"].id\" value='"+nodesObj[j].id+"'/><input type=\"radio\" name=\"stepindex\" value=\""+stepTable.rows.length+"\" onclick=\"changeStep('"+stepTable.rows.length+"')\"/>";
							var newTd1 = newTr.insertCell(1);
							newTd1.innerHTML = "<input type=\"hidden\" id='description"+stepTable.rows.length+"' name=\"details["+stepTable.rows.length+"].description\" value='"+nodesObj[j].description+"'/>"+nodesObj[j].description;
							var newTd2 = newTr.insertCell(2);
							newTd2.innerHTML = "<input type=\"hidden\" id='allAuth"+stepTable.rows.length+"' name=\"details["+stepTable.rows.length+"].allAuth\" value='"+nodesObj[j].allAuth+"'/>"+nodesObj[j].allAuthName;
							var newTd3 = newTr.insertCell(3);
							newTd3.innerHTML = "<input type=\"hidden\" id='dealUsers"+stepTable.rows.length+"' name=\"details["+stepTable.rows.length+"].dealUsers\" value='"+nodesObj[j].dealUsers+"'/>"+nodesObj[j].dealUsersName;
							
						}
						
					}
				});
				
				//部门树折叠
				 $('#dealUsers888').combotree('tree').tree("collapseAll");
				$('#dlg').dialog('setTitle', '修改流程资料').dialog('open');
				$.each($('#myform input'),function(i){
					$(this).removeAttr("readonly");
				});
				$("#dlg-buttons a::first-child").show();
				actionUrl = '<c:url value="/permissions/updateFlowDefine.tg"/>';
			}else{
				alert('请选则一套流程修改');
			}
		}
		function delItem(){
			var t = $('#t-flow');
			var row = t.datagrid('getSelected');
			if(row){
				$('#flowId').val(row.id);
				actionUrl = '<c:url value="/permissions/delFlowDefine.tg"/>';
				$('#flowform').form('submit', {
					url:actionUrl,
					success: function(data){
						$('#dlg').dialog('close');
						$('#t-flow').datagrid('reload');
						data=eval('('+data+')');
						if(data.success){
							$.messager.show(
								{
									title:'提示',
									msg:'操作成功！',
									showType:'slide'
								}
							);
						}
						if(data.error){
							$.messager.alert('提示','操作失败!'+data.message,'error');
						}
					}
				});
			}else{
				alert('请选择要删除的流程！谢谢');
			}
		}
		function saveItem(){
			$('#myform').form('submit', {
				url:actionUrl,
				onSubmit: function(){
					return $('#myform').form('validate');
				},
				success: function(data){
					$('#dlg').dialog('close');
					$('#t-flow').datagrid('reload');
					data=eval('('+data+')');
					if(data.success){
						$.messager.show(
							{
								title:'提示',
								msg:'操作成功！',
								showType:'slide'
							}
						);
					}
					if(data.error){
						$.messager.alert('提示','操作失败！','error');
					}
				}
			});
		}
		var stepCount = 0;
		function newDetailItem(){
			var stepTable = document.getElementById('stepTable');
			var newTr = stepTable.insertRow(stepTable.rows.length);
			if(stepCount==0)
			{
				stepCount = stepTable.rows.length+1;
			}
			newTr.id='row_'+stepCount;
			var newTd0 = newTr.insertCell(0);						
			newTd0.innerHTML = "<input type=\"hidden\" id='id"+stepCount+"' name=\"details["+stepCount+"].id\" value='0'/><input type=\"radio\" name=\"stepindex\" value=\""+stepCount+"\" onclick=\"changeStep('"+stepCount+"')\"/>";
			var newTd1 = newTr.insertCell(1);
			newTd1.innerHTML = "<input type=\"hidden\" id='description"+stepCount+"' name=\"details["+stepCount+"].description\" value='"+$('#description888').val()+"'/>"+$('#description888').val();
			var newTd2 = newTr.insertCell(2);
			newTd2.innerHTML = "<input type=\"hidden\" id='allAuth"+stepCount+"' name=\"details["+stepCount+"].allAuth\" value='"+$('#allAuth888').combobox('getValue')+"'/>"+$('#allAuth888').combobox('getText');
			var newTd3 = newTr.insertCell(3);
			//newTd3.innerHTML = "<input type=\"hidden\" id='dealUsers"+stepCount+"' name=\"details["+stepCount+"].dealUsers\" value='"+$('#dealUsers888').combotree('getValue')+"'/>"+$('#dealUsers888').combotree('getText');
			newTd3.innerHTML = "<input type=\"hidden\" id='dealUsers"+stepCount+"' name=\"details["+stepCount+"].dealUsers\" value='"+$('#dealUsers888').combotree('getValues')+"'/>"+
			"<input type=\"hidden\" id='dealUsersText"+stepCount+"' name=\"dealUsersText"+stepCount+"\" value='"+$('#dealUsers888').combotree('getText')+"'/>"+
			$('#dealUsers888').combotree('getText');
			
			
			stepCount = stepCount + 1;
			//部门树折叠
			 $('#dealUsers888').combotree('tree').tree("collapseAll");
		}
		function changeStep(stepNo)
		{
			$('#id888').val(document.getElementById('id'+stepNo).value);
			$('#description888').val(document.getElementById('description'+stepNo).value);
			$('#allAuth888').combobox('setValue',document.getElementById('allAuth'+stepNo).value);
			//$('#dealUsers888').combotree('tree').tree("collapseAll");
			//alert(document.getElementById('dealUsers'+stepNo).value);
			$('#dealUsers888').combotree('setValues',document.getElementById('dealUsers'+stepNo).value.split(","));
			$('#dealUsers888').combotree('setText',document.getElementById('dealUsersText'+stepNo).value);
			
			//部门树折叠
			
		}
		function delDetailItem()
		{
			var stepIndexs=document.getElementsByName("stepindex");
			var stepindex='-1';
			for(var i=0;i<stepIndexs.length;i++)
			{
			     if(stepIndexs[i].checked)
			    	 stepindex=stepIndexs[i].value;
			}
			if(stepindex=='-1')
			{
				 alert('请选择一个要删除的节点!');
				 return false;
			}
			$('#id888').val('');
			$('#description888').val('');
			$('#allAuth888').combobox('clear');
			$('#dealUsers888').combotree('clear');
			
			var delRow = document.getElementById('row_'+stepindex);
			var stepTable = document.getElementById('stepTable');
			stepTable.deleteRow(delRow.rowIndex);
		}
		function editDetailItem(){
			
			var stepTable = document.getElementById('stepTable');			
			
			var stepIndexs=document.getElementsByName("stepindex");
			var stepindex='-1';
			for(var i=0;i<stepIndexs.length;i++)
			{
			     if(stepIndexs[i].checked)
			    	 stepindex=stepIndexs[i].value;
			}
			if(stepindex=='-1')
			{
				 alert('请选择一个要更新的节点!');
				 return false;
			}			
			var updateRow = document.getElementById('row_'+stepindex);
			var rowIndex = updateRow.rowIndex;
			
			stepTable.rows[rowIndex].cells[0].innerHTML = "<input type=\"hidden\" id='id"+stepindex+"' name=\"details["+stepindex+"].id\" value='"+$('#id888').val()+"'/><input type=\"radio\" name=\"stepindex\" value=\""+stepindex+"\" onclick=\"changeStep('"+stepindex+"')\"/>";
			stepTable.rows[rowIndex].cells[1].innerHTML = "<input type=\"hidden\" id='description"+stepindex+"' name=\"details["+stepindex+"].description\" value='"+$('#description888').val()+"'/>"+$('#description888').val();
			stepTable.rows[rowIndex].cells[2].innerHTML = "<input type=\"hidden\" id='allAuth"+stepindex+"' name=\"details["+stepindex+"].allAuth\" value='"+$('#allAuth888').combobox('getValue')+"'/>"+$('#allAuth888').combobox('getText');
			stepTable.rows[rowIndex].cells[3].innerHTML = "<input type=\"hidden\" id='dealUsers"+stepindex+"' name=\"details["+stepindex+"].dealUsers\" value='"+$('#dealUsers888').combotree('getValues')+"'/>"+
			"<input type=\"hidden\" id='dealUsersText"+stepindex+"' name=\"dealUsersText"+stepindex+"\" value='"+$('#dealUsers888').combotree('getText')+"'/>"+
			$('#dealUsers888').combotree('getText');

		}		
	</script>